import pygame

class BattleSystem:
    def __init__(self, player, enemy):
        self.player = player
        self.enemy = enemy
        self.current_turn = "player"
        self.font = pygame.font.Font(None, 36)
        self.battle_ended = False
        self.battle_result = ""
        
        # 添加技能按钮
        self.skill1_button = pygame.Rect(50, 400, 120, 40)
        self.skill2_button = pygame.Rect(200, 400, 120, 40)
        self.player_choice = None  # 记录玩家选择
        self.enemy_choice = None   # 记录AI选择

    def draw(self, screen):
        # 绘制精灵图片
        screen.blit(self.player.image, (100, 200))
        screen.blit(self.enemy.image, (600, 200))
        
        # 绘制生命值条
        self.draw_health_bars(screen)
        
        # 绘制技能按钮
        if self.current_turn == "player" and not self.battle_ended:
            pygame.draw.rect(screen, (150, 150, 150), self.skill1_button)
            pygame.draw.rect(screen, (150, 150, 150), self.skill2_button)
            skill1_text = self.font.render("ATTACK", True, (0, 0, 0))
            skill2_text = self.font.render("HEAL", True, (0, 0, 0))
            screen.blit(skill1_text, (self.skill1_button.x + 20, self.skill1_button.y + 10))
            screen.blit(skill2_text, (self.skill2_button.x + 30, self.skill2_button.y + 10))
        
        if self.battle_ended:
            self.draw_battle_result(screen)

    def handle_click(self, pos):
        if self.battle_ended:
            ok_button = pygame.Rect(350, 280, 100, 40)
            if ok_button.collidepoint(pos):
                return True
                
        if self.current_turn == "player":
            if self.skill1_button.collidepoint(pos):
                self.player_choice = "attack"
                self.enemy_choice = self.get_enemy_choice()  # 获取AI选择
                self.execute_turn()  # 执行回合
            elif self.skill2_button.collidepoint(pos):
                self.player_choice = "heal"
                self.enemy_choice = self.get_enemy_choice()
                self.execute_turn()
        return False

    def get_enemy_choice(self):
        # AI决策逻辑
        if self.enemy.current_hp < self.enemy.max_hp * 0.5:
            return "heal"
        else:
            return "attack"

    def execute_turn(self):
        # 初始化伤害和治疗量
        player_damage = 0
        enemy_damage = 0
        player_heal = 0
        enemy_heal = 0

        # 计算玩家行动效果
        if self.player_choice == "attack":
            enemy_damage = self.player.attack
        elif self.player_choice == "heal":
            player_heal = self.player.attack * 0.2

        # 计算敌方行动效果
        if self.enemy_choice == "attack":
            player_damage = self.enemy.attack
        elif self.enemy_choice == "heal":
            enemy_heal = self.enemy.attack * 0.2

        # 应用所有效果
        self.player.current_hp = max(0, min(
            self.player.max_hp,
            self.player.current_hp - player_damage + player_heal
        ))
        self.enemy.current_hp = max(0, min(
            self.enemy.max_hp,
            self.enemy.current_hp - enemy_damage + enemy_heal
        ))

        # 检查战斗是否结束
        self.check_battle_end()
        if not self.battle_ended:
            self.current_turn = "player"  # 下一回合

    def check_battle_end(self):
        # 当血量<=0时结束游戏
        if self.player.current_hp <= 0:
            self.player.current_hp = 0  # 强制设为0
            self.battle_ended = True
            self.battle_result = "DEFEAT"
            return True
        elif self.enemy.current_hp <= 0:
            self.enemy.current_hp = 0  # 强制设为0
            self.battle_ended = True
            self.battle_result = "VICTORY"
            return True
        return False

    def draw_battle_result(self, screen):
        # 创建结果窗口
        result_box = pygame.Rect(250, 200, 300, 150)
        pygame.draw.rect(screen, (200, 200, 200), result_box)
        pygame.draw.rect(screen, (100, 100, 100), result_box, 3)

        # 显示战斗结果
        result_text = self.font.render(self.battle_result, True, (0, 0, 0))
        screen.blit(result_text, (result_box.centerx - result_text.get_width()//2, 
                                 result_box.y + 30))

        # 显示确定按钮
        ok_button = pygame.Rect(350, 280, 100, 40)
        pygame.draw.rect(screen, (150, 150, 150), ok_button)
        ok_text = self.font.render("OK", True, (0, 0, 0))
        screen.blit(ok_text, (ok_button.centerx - ok_text.get_width()//2, 
                             ok_button.centery - ok_text.get_height()//2))

    def draw_health_bars(self, screen):
        # 玩家生命值
        pygame.draw.rect(screen, (255, 0, 0), (100, 150, 100, 20))
        health_width = (self.player.current_hp / self.player.max_hp) * 100
        pygame.draw.rect(screen, (0, 255, 0), (100, 150, health_width, 20))

        # 敌人生命值
        pygame.draw.rect(screen, (255, 0, 0), (600, 150, 100, 20))
        health_width = (self.enemy.current_hp / self.enemy.max_hp) * 100
        pygame.draw.rect(screen, (0, 255, 0), (600, 150, health_width, 20))